package com.yuyou.fn.portal.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.service.ISmsService;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.RegexUtils;
import com.yuyou.fn.common.util.Threads;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.service.ICustomerUserService;
import com.yuyou.fn.portal.entity.OuterFeedback;
import com.yuyou.fn.portal.mapper.OuterFeedbackMapper;
import com.yuyou.fn.portal.service.IOuterFeedbackService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 *
 * OuterFeedback 表数据服务层接口实现类
 *
 */
@Service
public class OuterFeedbackServiceImpl extends SuperServiceImpl<OuterFeedbackMapper, OuterFeedback,Long> implements IOuterFeedbackService {

    private static final Logger log = LoggerFactory.getLogger(OuterFeedbackServiceImpl.class);

    @Resource
    private ICustomerUserService customerUserService;

    @Resource
    private ISmsService smsService;

    @Override
    public void contactUs(OuterFeedback outerFeedback) {
        outerFeedback.setCreateTime(TimeUtils.now());
        outerFeedback.setUpdateTime(TimeUtils.now());
        outerFeedback.setUsername(customerUserService.findById(outerFeedback.getUserId(),"username").getUsername());
        outerFeedback.setStatus(0);//未处理
        this.insertSelective(outerFeedback);
    }

    @Override
    public Page<OuterFeedback> findForPage(String username, Date start, Date end, Integer status, int pageNo, int pageSize) {

        Example<OuterFeedback> example = OuterFeedback.createExample();
        example.orderBy("createTime").desc();

        Example.Criteria criteria = example.createCriteria();

        if(status != null ){
            criteria.andEqualTo("status",status);
        }

        if(StringUtils.isNotEmpty(username)){
            criteria.andEqualTo("username",username);
        }

        if(start != null ){
            criteria.andGreaterThan("createTime",start);
        }

        if(end != null ){
            criteria.andLessThan("createTime" , DateUtils.addDays(end,1));
        }

        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public void updateFeedbackStatus(Long outerFeedbackId, Integer status, String contact, String replyContent) {
        if (!RegexUtils.isPhone(contact)){
            throw new BusException("手机号码格式不正确！");
        }

        Threads.dispatch(()->{
            try {
                smsService.send(Arrays.asList(contact),replyContent);
            } catch (Exception e) {
                log.error("Send feedBack msg error :" ,e);
            }
        });
        Principals.Principal principal = Principals.get();
        if (null == principal){
            throw new BusException("操作人为空！");
        }
        Long operatorId = principal.id;
        String opratorName = principal.name;
        OuterFeedback outerFeedback = new OuterFeedback() ;
        outerFeedback.setOuterFeedbackId(outerFeedbackId);
        outerFeedback.setStatus(status);
        outerFeedback.setOperatorId(operatorId);
        outerFeedback.setOperatorName(opratorName);
        outerFeedback.setReplyContent(replyContent);
        outerFeedback.setReplyTime(TimeUtils.now());
        updateSelectiveById(outerFeedback);
    }


}